package com.springcloud.sc.ucpv5.client.application;

import com.springcloud.sc.ucpv5.client.application.message.DataMessageManager;
import com.springcloud.sc.ucpv5.client.application.message.MessageService;
import com.springcloud.sc.ucpv5.client.application.message.MessageServiceException;
import com.springcloud.sc.ucpv5.client.application.message.MesssageHandle;
import com.springcloud.sc.ucpv5.client.commondata.MyUser;
import com.springcloud.sc.ucpv5.client.commondata.SecuResult;
import com.springcloud.sc.ucpv5.client.commondata.data.LoginRsp;
import com.springcloud.sc.ucpv5.client.commondata.data.User;
import com.springcloud.sc.ucpv5.client.module.datamanager.BaseDataManager;
import com.springcloud.sc.ucpv5.client.module.datamanager.LoginDataManager;

public class ApplicationUtil {
    /**
     * 登录状态
     */
    private static boolean login;

    public static boolean isLogin() {
	return login;
    }

    public static void setLogin(boolean a) {
	login = a;
    }

	public static boolean initCnmsSystem() {
		// 1.初始化连接
		int serverPort = Integer.valueOf(com.springcloud.sc.ucpv5.client.module.datamanager.LoginDataManager.getInstance().getServerPort());
		String serverIp = com.springcloud.sc.ucpv5.client.module.datamanager.LoginDataManager.getInstance().getServerIp();
		try {
			System.out.println("开始初始化连接");
			int retCode = MessageService.getInstance().init(serverIp, serverPort);
			System.out.println("初始化连接返回:" + retCode);
			if (com.springcloud.sc.ucpv5.client.commondata.SecuResult.RET_SUCCEED != retCode) {
				if (retCode != -1) {
				} else {
				}
				return false;
			}
		} catch (MessageServiceException e) {
			e.printStackTrace();
			System.out.println("初始化连接返回异常");
			return false;
		}
		com.springcloud.sc.ucpv5.client.commondata.SecuResult result = new com.springcloud.sc.ucpv5.client.commondata.SecuResult();
		System.out.println("开始登陆");

		String userName = com.springcloud.sc.ucpv5.client.module.datamanager.LoginDataManager.getInstance().getName();
		String userPassword = com.springcloud.sc.ucpv5.client.module.datamanager.LoginDataManager.getInstance().getPassWord();

		System.out.println("登录用户名:" + userName + "," + userPassword);
		LoginRsp rsp = DataMessageManager.getInstance().LoginSystem(userName, userPassword, result, true, true);
		if (rsp == null) {
			System.out.println("登陆失败 rsp Null");
			return false;
		}
		if (rsp.Result != SecuResult.RET_SUCCEED) {
			System.out.println("登陆失败:" + rsp.Result);
			return false;
		} else {
			System.out.println("登陆成功");
			String[] tmp = rsp.Auth_NodeID_List.split("##");
			String other = null;
			if (tmp.length == 2) {
				other = tmp[0];
			} else if (tmp.length == 1) {
				// 表示带其他和 不带FriendNodeList
				other = tmp[0];
			}
			String[] tmp1 = other.split("#");
			if (tmp1.length >= 1) {
				// 电话号码属于第一个
				LoginDataManager.getInstance().setUserTelphone(tmp1[0]);
			}
		}
		login = true;
		// 开始加载数据
		loadModule();
		return true;
	}

	public static boolean loadModule() {

		try {
			MesssageHandle.getInstance();
			LoginDataManager.getInstance().initDcsUser();

		} catch (Throwable e) {
			e.printStackTrace();
		}

		try {
			com.springcloud.sc.ucpv5.client.commondata.MyUser user = LoginDataManager.getInstance().getUser();
			if (user == null) {
				User tmp = new User();
				tmp.UserName = LoginDataManager.getInstance().getName();
				tmp.UserTelno = LoginDataManager.getInstance().getUserTelphone();

				MyUser user_ = new MyUser(tmp, MessageService.getInstance().getPhysicsNodeNumber(), MessageService.getInstance().getLogicNodeNumber(),
						0);

				// 出发查询客户自生详细信息(如果已存在,则不触发查询)
				SecuResult result = DataMessageManager.getInstance().queryUserInfo(MessageService.getInstance().getPhysicsNodeNumber(),
						MessageService.getInstance().getLogicNodeNumber(), user_, false, false);

				if (result.getRetCode() == 0) {
					LoginDataManager.getInstance().setUser(user_);
				} else {
					System.out.println("查询用户详细失败!!!!");
					DataMessageManager.getInstance().queryUserInfo_Async(MessageService.getInstance().getPhysicsNodeNumber(),
							MessageService.getInstance().getLogicNodeNumber(), user_);
				}
			} else {
				// 出发查询客户自生详细信息(如果已存在,则不触发查询)
				user.getUserInfo();
			}
			BaseDataManager.getInstance().getLoginNode().getListModel().addUser(user);
		}catch (Throwable e) {
			e.printStackTrace();
		}

		try {
			// 出发加载节点数据
			SecuResult result = BaseDataManager.getInstance().nodeLoadData(BaseDataManager.getInstance().getLoginNode());

			if (result.getRetCode() != 0) {
				System.out.println("查询节点数据失败!!!!");
			}
		}catch (Throwable e) {
			e.printStackTrace();
		}
		try {

		}catch (Throwable e) {
			e.printStackTrace();
		}
		try {

		}catch (Throwable e) {
			e.printStackTrace();
		}
		return true;
	}
}
